package com.lys.controller;

import com.lys.entity.ProductCategory;
import com.lys.param.ProductCategoryParam;
import com.lys.service.ProductCategoryService;
import com.lys.service.ProductService;
import com.lys.utils.Constants;
import com.lys.utils.EmptyUtils;
import com.lys.utils.Pager;
import com.lys.utils.ReturnResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 陆小根
 * @version 9.0.4
 * @Date 2022/5/30 8:45
 */
@Controller
@RequestMapping("/productCategory")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;
    @Autowired
    private ProductService productService;

    /**
     * 订单的主页面
     * @return
     */
    @GetMapping("/index")
    public String index(Model model,
                        @RequestParam(value = "currentPage",required = false) String currentPageStr,
                        @RequestParam(value = "pageSize",required = false) String pageSize){
        //获取当前页数
        //String currentPageStr = request.getParameter("currentPage");
        //获取页大小
        //String pageSize = request.getParameter("pageSize");
        int rowPerPage  = EmptyUtils.isEmpty(pageSize)?8:Integer.parseInt(pageSize);
        int currentPage = EmptyUtils.isEmpty(currentPageStr)?1:Integer.parseInt(currentPageStr);
        ProductCategoryParam params =new ProductCategoryParam();
        int total=productCategoryService.queryProductCategoryCount(params);
        Pager pager=new Pager(total,rowPerPage,currentPage);
        params.openPage((pager.getCurrentPage()-1)*pager.getRowPerPage(),pager.getRowPerPage());
        pager.setUrl("productCategory/index"); // ?????
        List<ProductCategory> productCategoryList=productCategoryService.queryProductCategorylistBySql(params);
        model.addAttribute("productCategoryList", productCategoryList);
        model.addAttribute("pager", pager);
        model.addAttribute("menu", 4);
        return "/backend/productCategory/productCategoryList";
    }
    /**
     * 添加商品分类
     * @return
     */
    @GetMapping("/toAdd")
    public String toAddProductCategory(Model model){
        //查询一级分类 全部
        List<ProductCategory> productCategoryList=null;
        ProductCategoryParam params =new ProductCategoryParam();
        params.setType(1);
        productCategoryList=productCategoryService.queryProductCategoryList(params);
        model.addAttribute("productCategoryList1",productCategoryList);
        model.addAttribute("productCategory",new ProductCategory());
        return "/backend/productCategory/toAddProductCategory";
    }
    /**
     * 修改商品分类
     * @return
     */
    @GetMapping("/toUpdate")
    public String toUpdateProductCategory(@RequestParam("id") String id,
                                          Model model) {
        // String id=request.getParameter("id");
        ProductCategory productCategory=productCategoryService.getById(Integer.parseInt(id));
        List<ProductCategory> productCategoryList1=null;
        List<ProductCategory> productCategoryList2=null;
        List<ProductCategory> productCategoryList3=null;
        model.addAttribute("productCategory",productCategory);
        //判断分类级别
        if(productCategory.getType()>=1){
            ProductCategoryParam params =new ProductCategoryParam();
            params.setType(1);
            productCategoryList1=productCategoryService.queryProductCategoryList(params);
        }
        if(productCategory.getType()>=2){
            ProductCategoryParam params =new ProductCategoryParam();
            params.setType(2);
            productCategoryList2=productCategoryService.queryProductCategoryList(params);
            model.addAttribute("parentProductCategory",productCategoryService.getById(productCategory.getParentId()));
        }
        if(productCategory.getType()>=3){
            List<ProductCategory> productCategoryList=null;
            ProductCategoryParam params =new ProductCategoryParam();
            params.setType(3);
            productCategoryList3=productCategoryService.queryProductCategoryList(params);
        }
        model.addAttribute("productCategoryList1",productCategoryList1);
        model.addAttribute("productCategoryList2",productCategoryList2);
        model.addAttribute("productCategoryList3",productCategoryList3);
        return "/backend/productCategory/toAddProductCategory";
    }

    /**
     * 查询子分类
     * @throws Exception
     */
    @PostMapping("/query")
    @ResponseBody
    public ReturnResult queryProductCategoryList(@RequestParam("parentId") String parentId) {
        ReturnResult result=new ReturnResult();
        // String parentId=request.getParameter("parentId");
        List<ProductCategory> productCategoryList=null;
        ProductCategoryParam params =new ProductCategoryParam();
        params.setParentId(EmptyUtils.isEmpty(parentId)?0:Integer.parseInt(parentId));
        productCategoryList=productCategoryService.queryProductCategoryList(params);
        result.setStatus(Constants.ReturnResult.SUCCESS);
        result.setData(productCategoryList);
        return result;
    }
    /**
     * 修改商品分类
     * @param request
     * @return
     * @throws Exception
     *
     *
     * 待测试
     */
    @PostMapping("/modify")
    @ResponseBody
    public ReturnResult modifyProductCategory(HttpServletRequest request,ProductCategory productCategory1){
        ReturnResult result=new ReturnResult();
        Integer parentId=0;
        String id=request.getParameter("id");
        String productCategoryLevel1=request.getParameter("productCategoryLevel1");
        String productCategoryLevel2=request.getParameter("productCategoryLevel2");
        String name=request.getParameter("name");
        String type=request.getParameter("type");
        if(type.equals("1")){
            parentId =0;
        }else if(type.equals("2")){
            parentId =Integer.parseInt(productCategoryLevel1);
        }else{
            parentId =Integer.parseInt(productCategoryLevel2);
        }
        ProductCategory productCategory  =new ProductCategory();
        productCategory.setId(Integer.parseInt(id));
        productCategory.setParentId(parentId);
        productCategory.setName(name);
        productCategory.setType(Integer.parseInt(type));
        productCategoryService.modifyProductCategory(productCategory);
        result.returnSuccess();
        return result;
    }
    /**
     * 添加商品分类
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/save")
    @ResponseBody
    public ReturnResult addProductCategory(HttpServletRequest request,
                                           ProductCategory productCategory) {
        ReturnResult result=new ReturnResult();
        Integer parentId=0;
        //获取分类级别
//        String type=request.getParameter("type");
        String type=productCategory.getType().toString();
        String productCategoryLevel1=request.getParameter("productCategoryLevel1");
        String productCategoryLevel2=request.getParameter("productCategoryLevel2");
        String name=request.getParameter("name");
        if(type.equals("1")){
            parentId =0;
        }else if(type.equals("2")){
            parentId =Integer.parseInt(productCategoryLevel1);
        }else{
            parentId =Integer.parseInt(productCategoryLevel2);
        }
        //productCategory.setName(name);
        productCategory.setParentId(parentId);
        //productCategory.setType(Integer.parseInt(type));
        productCategory.setIconClass("");
        productCategoryService.addProductCategory(productCategory);
        result.returnSuccess();
        return result;
    }
    /**
     * 删除商品分类
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    @ResponseBody
    public ReturnResult deleteProductCategory(@RequestParam("id") String id,
                                              @RequestParam("type") String type) {
        ReturnResult result=new ReturnResult();
        //获取分类id
        // String id=request.getParameter("id");
        // String type=request.getParameter("type");
        //查询是否有子类
        ProductCategoryParam productCategoryParam=new ProductCategoryParam();
        productCategoryParam.setParentId(Integer.parseInt(id));
        int count=productCategoryService.queryProductCategoryCount(productCategoryParam);
        if(count>0){
            return result.returnFail("已经存在子分类，不能删除");
        }
        //查询是否有商品
        count=productService.count(null,Integer.parseInt(id),null);
        if(count>0){
            return result.returnFail("已经存在商品，不能删除");
        }
        productCategoryService.deleteById(Integer.parseInt(id));
        result.returnSuccess();
        return result;
    }

}
